package com.parkingspace.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.dao.ParkingInformationMapper;
import com.parkingspace.dao.UserMapper;
import com.parkingspace.entity.ParkingInformation;
import com.parkingspace.entity.User;
import com.parkingspace.service.*;
import com.parkingspace.util.JwtUtils;
import com.parkingspace.util.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 车位信息表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@Service
@Slf4j
public class ParkingInformationServiceImpl extends ServiceImpl<ParkingInformationMapper, ParkingInformation> implements IParkingInformationService {

	@Resource
	private ParkingInformationMapper parkingInformationMapper;

	@Resource
	private UserMapper userMapper;

	@Resource
	private IEPropertyService iePropertyService;

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	@Resource
	private IEUserService ieUserService;

	@Override
	public Map<Object, Object> findAllIn(String token, long current, long size, Long propertyId, ParkingInformation parkingInformation) throws Exception {
		log.info("执行findAllIn方法传入的ParkingInformation的值为:{}",parkingInformation.toString());
		Map<Object, Object> map = new HashMap<>();
		String userName = JwtUtils.getUsername(token);
		QueryWrapper<ParkingInformation> queryWrapper = new QueryWrapper<>();
		if (userName != null) {
			//查询出用户ID并装入eq条件
			QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
			queryWrapper2.eq("uname",userName);
			User user = userMapper.selectOne(queryWrapper2);
			queryWrapper.eq("parking_uid",user.getUid());
		}
		if (propertyId != null) {
			//装入物业ID
			queryWrapper.eq("property_id",propertyId);
		}
		//判定parkingInformation对象是否为空
		if (parkingInformation != null) {
			//车位编号查询
			if (parkingInformation.getParkingId() != null) {
				queryWrapper.eq("parking_id", parkingInformation.getParkingId());
			}
			//车位状态
			if (parkingInformation.getParkingStatus() != null) {
				queryWrapper.eq("parking_status", parkingInformation.getParkingStatus());
			}
			//车位地址模糊查询
			if (parkingInformation.getParkingAddress() != null) {
				queryWrapper.like("parking_address", "%"+parkingInformation.getParkingAddress()+"%");
			}
			//分成比例
			if (parkingInformation.getParkingProportion() != null) {
				queryWrapper.eq("parking_proportion", parkingInformation.getParkingProportion());
			}
			//是否已录入车牌号码到物业
			if (parkingInformation.getLicensePlateNumberEntering() != null) {
				queryWrapper.eq("license_plate_number_entering", parkingInformation.getLicensePlateNumberEntering());
			}
			//车位价格
			if (parkingInformation.getParkingRent() != null) {
				queryWrapper.eq("parking_rent", parkingInformation.getParkingRent());
			}
		}

		//将所有用户的信息都装入map
		ResultVO all = ieUserService.findAll();
		map.put("yonghu",all.getData());

		//将所有的物业信息装入map
		ResultVO all1 = iePropertyService.findAll();
		map.put("wuye",all1.getData());

		Page<ParkingInformation> parkingInformationPage = parkingInformationMapper.selectPage(new Page<ParkingInformation>(current, size), queryWrapper);
			map.put("chewei",parkingInformationPage);
			return map;
	}

	@Override
	public Boolean shelfSpace(String token, Long parkingId, Integer status, double rent) throws Exception {
		int[] arr = {0,1,2,3,4};
		if (! (Arrays.binarySearch(arr, status) > 0)) {
			throw new RuntimeException("传入状态数字非法!");
		}
		//查询钱包金额是否大于200
		String username = JwtUtils.getUsername(token);
		Object o = redisTemplate.opsForValue().get(username);
		if (o == null) {
			throw new RuntimeException("请先进行登录!");
		}

		ResultVO resultVO = ieUserService.queryWalletAmount(token, (Integer) o);
		Double aDouble = (Double) resultVO.getData();
		if (aDouble < 200) {
			throw new RuntimeException("请先确保钱包的金额不小于200元!");
		}
		QueryWrapper<ParkingInformation> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parking_id", parkingId);
		ParkingInformation parkingInformation = new ParkingInformation();
		parkingInformation.setParkingStatus(status);
		parkingInformation.setParkingRegistrationTime(DateUtil.date());
		parkingInformation.setParkingRent(rent);
		boolean b = parkingInformationMapper.update(parkingInformation, queryWrapper) == 1;
		if (!b) {
			throw new RuntimeException("上架车位异常!");
		}
		return b;
	}

	@Override
	public Boolean shelvesParking(Long parkingId) throws Exception{
		ParkingInformation parkingInformation = parkingInformationMapper.selectById(parkingId);
		if (parkingInformation == null) {
			throw new RuntimeException("车位ID输入错误!");
		}
		if (parkingInformation.getCarUid() != null && parkingInformation.getParkingStatus() != 2) {
			throw new RuntimeException("下架失败,当前车位尚有订单未处理完成!");
		}
		QueryWrapper<ParkingInformation> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parking_id", parkingInformation.getParkingId());

		ParkingInformation p = new ParkingInformation();
		p.setParkingStatus(1);
		boolean b = parkingInformationMapper.update(p, queryWrapper) == 1;
		if (!b) {
			throw new RuntimeException("下架车位异常!");
		}
		return b;
	}
}
